<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/logo.png" alt="logo" />
            <div class="title">YT云图库</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto" v-if="route.name !== 'UserLogin' && route.name !== 'UserRegister'">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="headerItems"
          @click="doMenuClick"
        />
      </a-col>
      <a-col flex="120px">
        <div
          v-if="route.name !== 'UserLogin' && route.name !== 'UserRegister'"
          style="display: flex; align-items: center"
        >
          <a-dropdown>
            <a-space>
              <a-avatar :src="nowLoginUser.userAvatar" />
              <div v-if="nowLoginUser.id">
                {{ nowLoginUser.userName ?? '无名' }}
              </div>
              <div v-else>
                <a-button type="primary" href="/user/login">登录</a-button>
              </div>
            </a-space>
            <template #overlay>
              <a-menu>
                <a-menu-item @click="doLogout">
                  <LogoutOutlined />
                  退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script setup lang="ts">
import { computed, h, ref } from 'vue'
import { HomeOutlined, LogoutOutlined, ApartmentOutlined } from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserLoginStore } from '@/stores/useLoginUserStore.ts'
import { userLogoutUsingPost } from '@/api/userController.ts'

const loginUserStore = useUserLoginStore()
const nowLoginUser = computed(() => loginUserStore.loginUser)

const headerItems = ref<MenuProps['items']>([
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/user/manager',
    icon: () => h(ApartmentOutlined),
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/about',
    label: '关于',
    title: '关于',
  },
  {
    key: '/others',
    label: h('a', { href: 'https://antdv.com', target: '_blank' }, '其他'),
    title: '其他',
  },
])

const router = useRouter()
const route = useRoute()

const doMenuClick = ({ key }: any) => {
  router.push({ path: key })
}

const current = ref<string[]>(['/'])
router.afterEach((to, from) => {
  current.value = [to.path]
})

const doLogout = async () => {
  const res = await userLogoutUsingPost();
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      id: undefined,
      userName: '未登录'
    });
    message.success("退出登录成功");
    await router.push("/user/login")
  } else {
    message.error("退出登录失败：" + res.data.message)
  }
}
</script>

<style scoped>
#globalHeader {
  .title-bar {
    display: flex;
    align-items: center;
  }

  .title {
    color: black;
    font-size: 30px;
    margin-left: 16px;
  }
}

.logo {
  height: 48px;
}
</style>
